package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.entiey.Category;
import com.leyou.item.entiey.CategoryBrand;
import com.leyou.item.mapper.CategoryMapper;
import com.leyou.item.service.CategoryBrandService;
import com.leyou.item.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private CategoryBrandService categoryBrandService;

    @Override
    public List<CategoryDTO> queryCategoryByBrandId(Long brandId) {
        //先根据id查询中间表  得到CategoryBrand的集合
        List<CategoryBrand> categoryBrandList = categoryBrandService
                .query().eq("brand_id", brandId).list();

        //获取category_id
        List<Long> idList = categoryBrandList.stream()
                .map(CategoryBrand::getCategoryId).collect(Collectors.toList());

/*        List<Long> idList = new ArrayList<>(categoryBrandList.size());
        for (CategoryBrand categoryBrand : categoryBrandList) {
            idList.add(categoryBrand.getBrandId());
        }*/

        //查询分类表
        List<Category> categories = this.listByIds(idList);

        //转化为dto
        List<CategoryDTO> categoryDTOList = CategoryDTO.convertEntityList(categories);

        return categoryDTOList;
    }


}
